package com.sg.service.biz.component.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.dto.biz.component.req.AnalysisLevelCodeReqDto;
import com.sg.dto.biz.component.req.DeleteFileReqDto;
import com.sg.dto.biz.component.res.AnalysisLevelCodeRespDto;
import com.sg.dto.biz.component.res.DeleteFileRespDto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbCommon {


    private boolean isStartWithHttp(String str) {
        if (str != null && str.startsWith("http")) {
            // 去掉域名
            int index = str.indexOf("//");
            if (index != -1) {
                index = str.indexOf("/", index + 2);
                if (index != -1) {
                    str = str.substring(index);
                } else {
                    str = "/";
                }
            }
            return true;
        }
        return false;
    }

    /**
     * code:deleteFile
     * name:M2删除附件(特殊方法）
     * desc:undefined
     * gen by moon at 9/25/2023, 6:34:50 PM
     **/
    @Trace(operationName = "M2删除附件(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public DeleteFileRespDto deleteFile(DeleteFileReqDto reqDto) {
        String path = reqDto.getMediaContentAddress();
        System.out.println("src path : " + reqDto.getMediaContentAddress() + "\r\n");
        isStartWithHttp(path);
        path = "/www/server" + path;
        File file = new File(path);
        System.out.println("deal path : " + path + "\r\n");
        if (file.exists()) {
            System.out.println("hit path\r\n");
            file.delete();
        }
        return new DeleteFileRespDto();
    }

    /**
     * code:receptionService
     * name:M2接收排序值出参
     * desc:undefined
     * gen by moon at 7/23/2024, 8:14:00 PM
     **/
    @Trace(operationName = "M2接收排序值出参")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementTenReceivingFieldRespDto implementTenReceivingField(ImplementTenReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementTenReceivingFieldRespDto.class);
    }

    /**
     * code:getCurrentContentCode
     * name:M2分析层级标识（特殊方法）
     * desc:undefined
     * gen by moon at 7/23/2024, 8:14:18 PM
     **/
    @Trace(operationName = "M2分析层级标识（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public AnalysisLevelCodeRespDto analysisLevelCode(AnalysisLevelCodeReqDto reqDto) {
        AnalysisLevelCodeRespDto retData = new AnalysisLevelCodeRespDto();
        if (reqDto.getSiblingCodeList() == null || reqDto.getSiblingCodeList().size() == 0) {
            if (reqDto.getSuperiorContentCode().equals("1")) {
                retData.setCurrentContentCode(reqDto.getSuperiorContentCode() + "001");
            } else {
                retData.setCurrentContentCode(reqDto.getSuperiorContentCode() + "1001");
            }

        } else if (reqDto.getSiblingCodeList().size() > 0) {
            String first = reqDto.getSiblingCodeList().get(0);
            String preFix = first.substring(0, first.length() - 4);
            List<String> shortCodeList = reqDto.getSiblingCodeList().stream().map(code -> code.substring(code.length() - 4)).collect(Collectors.toList());
            List<Long> codeNumList = shortCodeList.stream().map(code -> Long.valueOf(code)).collect(Collectors.toList());
            Long max = Collections.max(codeNumList) + 1;
            retData.setCurrentContentCode(preFix + String.valueOf(max));
        }
        return retData;
    }
    //手工接入方法
}
